#!/bin/bash
#
# Script to perform some common system operations
#


function mainMenu {
clear
echo "***************************************************"
echo "* Mikes Tea Tips - Welcome                        *"
echo "***************************************************"
echo "*                       _______                   *"
echo "*    _..,----,.._      |_     _|.-----..---.-.    *"
echo "* .-;'-.,____,.-';       |   |  |  -__||  _  |    *"
echo "*(( |            |       |___|  |_____||___._|    *"
echo "* \`))            ;      _______ __                *"
echo "*  \` \          /      |_     _|__|.-----..-----. *"
echo "* .-' \`,.____.,' '-.     |   | |  ||  _  ||__ --| *"
echo "*(     '------'     )    |___| |__||   __||_____| *"
echo "* \`-=..________..--'               |__|           *"
echo "*                                                 *"
echo "***************************************************"
echo -n "Press Enter to continue. . ."
read
}

function gatherData {
join -j4 <(tail +3 $1 | sort -k4) <(tail +3 $2 | sort -k4) | cut -f 1,3,4,6,7 -d ' ' | tr ' ' ',' > temp.txt
rm -rf data.txt
for val in $(< temp.txt);do
instances1=$(echo $val | cut -d ',' -f 2)
bytes1=$(echo $val | cut -d ',' -f 3)
instances2=$(echo $val | cut -d ',' -f 4)
bytes2=$(echo $val | cut -d ',' -f 5)

let instancesDiff=instances1-instances2
let bytesDiff=bytes1-bytes2

echo $val,$instancesDiff,$bytesDiff | tr ',' ' ' >> data.txt
done
}


function sortBy {
    clear
    echo "***************************************************"
    echo "* Mikes Tea Tips - Result                         *"
    echo "***************************************************"
    cat <(echo "classname instances(1) bytes(1) instances(2) bytes(2) instancesDiff bytesDiff") <(sort -r -k $1 data.txt) | column -t 
    echo
    echo
    echo -n "Press Enter To Continue. . ."
    read -n 1 selection
}

function sortByMenu {
while :
do
clear
echo "***************************************************"
echo "* Mikes Tea Tips - Sort By                        *"
echo "***************************************************"
echo "*                                                 *"
echo "* 1) class name                                   *"
echo "* 2) instances (histo 1)                          *"
echo "* 3) bytes (histo 1)                              *"
echo "* 4) instances (histo 2)                          *"
echo "* 5) bytes (histo 2                               *"
echo "* 6) instances difference                         *"
echo "* 7) bytes differnece                             *"
echo "* X) exit                                         *"
echo "*                                                 *"
echo "***************************************************"
echo -n "Press Enter Selection. . ."
read -n 1 selection
case $selection in
1) sortBy $selection ;;
2) sortBy $selection ;;
3) sortBy $selection ;;
4) sortBy $selection ;;
5) sortBy $selection ;;
6) sortBy $selection ;;
7) sortBy $selection ;;
x) clear; exit 0 ;;
X) clear; exit 0 ;;
*) sortByMenu;;
esac
done
}





mainMenu
clear
echo "***************************************************"
echo "* Mikes Tea Tips                                  *" 
echo "* Please enter name of histo file 1:              *"
echo "***************************************************"
while :
do
read fileName1
if [ -f "$fileName1" ]; then
clear
echo "***************************************************"
echo "* Mikes Tea Tips - histo file 1 read              *" 
echo "* Please now enter name of histo file 2:          *"
echo "***************************************************"
    while :
    do
    read fileName2
    if [ -f "$fileName2" ]; then
        gatherData $fileName1 $fileName2
        sortByMenu
    fi
    echo "Error file $fileName2 did not exist please try again:"
    done
fi
echo "Error file $fileName1 did not exist please try again:"
done









